<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>JS拖拽元素原理及实现 | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../MyGitbook_new/js/Braces.html" />
    
    
    <link rel="prev" href="../../MyGitbook_new/js/Dom.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="4.4"
        data-chapter-title="JS拖拽元素原理及实现"
        data-filepath="MyGitbook_new/js/Drag_drop.md"
        data-basepath="../.."
        data-revision="Fri Dec 22 2017 15:27:24 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="MyGitbook_new/css/steps.html">
            
                
                    <a href="../../MyGitbook_new/css/steps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        手机端跳转js
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="MyGitbook_new/css/dassss.html">
            
            <span><b>1.1.</b> 第一天</span>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="MyGitbook_new/css/day1.html">
            
                
                    <a href="../../MyGitbook_new/css/day1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        3D动画
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="MyGitbook_new/js/regular.html">
            
                
                    <a href="../../MyGitbook_new/js/regular.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        正则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="MyGitbook_new/js/js.html">
            
                
                    <a href="../../MyGitbook_new/js/js.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        js
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="MyGitbook_new/js/function_js.html">
            
                
                    <a href="../../MyGitbook_new/js/function_js.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        函数的声明方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="MyGitbook_new/js/call_apply_bind.html">
            
                
                    <a href="../../MyGitbook_new/js/call_apply_bind.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        JS中改变this指向的方法(call和apply、bind)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="MyGitbook_new/js/Dom.html">
            
                
                    <a href="../../MyGitbook_new/js/Dom.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Dom入门教程图解
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.4" data-path="MyGitbook_new/js/Drag_drop.html">
            
                
                    <a href="../../MyGitbook_new/js/Drag_drop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        JS拖拽元素原理及实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="MyGitbook_new/js/Braces.html">
            
                
                    <a href="../../MyGitbook_new/js/Braces.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        {} 大括号
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="MyGitbook_new/js/shiti.html">
            
                
                    <a href="../../MyGitbook_new/js/shiti.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        试题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h3 id="js&#x62D6;&#x62FD;&#x5143;&#x7D20;&#x539F;&#x7406;&#x53CA;&#x5B9E;&#x73B0;">JS&#x62D6;&#x62FD;&#x5143;&#x7D20;&#x539F;&#x7406;&#x53CA;&#x5B9E;&#x73B0;</h3>
<pre><code>&#x62D6;&#x62FD;&#x529F;&#x80FD;&#x4E3B;&#x8981;&#x662F;&#x7528;&#x5728;&#x8BA9;&#x7528;&#x6237;&#x505A;&#x4E00;&#x4E9B;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x52A8;&#x4F5C;&#xFF0C;&#x6BD4;&#x5982;&#x62D6;&#x52A8;&#x6392;&#x5E8F;&#xFF0C;&#x5F39;&#x51FA;&#x6846;&#x62D6;&#x52A8;
&#x79FB;&#x52A8;&#x7B49;&#x7B49;&#xFF0C;&#x6548;&#x679C;&#x8FD8;&#x662F;&#x86EE;&#x4E0D;&#x9519;&#x7684;&#x3002;
</code></pre><h3 id="&#x62D6;&#x62FD;&#x7684;&#x6D41;&#x7A0B;&#x52A8;&#x4F5C;">&#x62D6;&#x62FD;&#x7684;&#x6D41;&#x7A0B;&#x52A8;&#x4F5C;</h3>
<pre><code>&#x2460; &#x9F20;&#x6807;&#x6309;&#x4E0B;
&#x2461; &#x9F20;&#x6807;&#x79FB;&#x52A8;
&#x2462; &#x9F20;&#x6807;&#x677E;&#x5F00;
</code></pre><h3 id="&#x62D6;&#x62FD;&#x6D41;&#x7A0B;&#x4E2D;&#x5BF9;&#x5E94;&#x7684;js&#x4E8B;&#x4EF6;">&#x62D6;&#x62FD;&#x6D41;&#x7A0B;&#x4E2D;&#x5BF9;&#x5E94;&#x7684;JS&#x4E8B;&#x4EF6;</h3>
<pre><code>&#x2460;&#x9F20;&#x6807;&#x6309;&#x4E0B;&#x4F1A;&#x89E6;&#x53D1;onmousedown&#x4E8B;&#x4EF6;
obj.onmousedown = function(e) {  
      //..........  
}
</code></pre><h3 id="&#x2461;&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x4F1A;&#x89E6;&#x53D1;onmousemove&#x4E8B;&#x4EF6;">&#x2461;&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x4F1A;&#x89E6;&#x53D1;onmousemove&#x4E8B;&#x4EF6;</h3>
<pre><code>obj.onmousemove = function(e) {  
    //......  
}
</code></pre><h3 id="&#x2462;&#x9F20;&#x6807;&#x677E;&#x5F00;&#x4F1A;&#x89E6;&#x53D1;onmouseup&#x4E8B;&#x4EF6;">&#x2462;&#x9F20;&#x6807;&#x677E;&#x5F00;&#x4F1A;&#x89E6;&#x53D1;onmouseup&#x4E8B;&#x4EF6;</h3>
<pre><code>obj.onmouseup = function() {  
     //......  
}
</code></pre><h3 id="&#x5B9E;&#x73B0;&#x7684;&#x539F;&#x7406;">&#x5B9E;&#x73B0;&#x7684;&#x539F;&#x7406;?</h3>
<pre><code>&#x62D6;&#x62FD;&#x5176;&#x5B9E;&#x662F;&#x901A;&#x8FC7;&#x83B7;&#x53D6;&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x7684;&#x8DDD;&#x79BB;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x5373;&#x8BA1;&#x7B97;&#x79FB;&#x52A8;&#x524D;&#x7684;&#x4F4D;&#x7F6E;&#x7684;&#x5750;&#x6807;&#xFF08;x&#xFF0C;y&#xFF09;
&#x4E0E;&#x79FB;&#x52A8;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x7684;&#x5750;&#x6807;&#xFF08;x&#xFF0C;y&#xFF09;&#x5DEE;&#x503C;&#x3002;
&#x9F20;&#x6807;&#x6309;&#x4E0B;&#x6216;&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x65F6;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;&#x5F53;&#x524D;&#x9F20;&#x6807;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x5373;&#x79FB;&#x52A8;&#x524D;&#x7684;&#x4F4D;&#x7F6E;&#x4E0E;&#x79FB;&#x52A8;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;.
</code></pre><pre><code class="lang-javascript"><span class="hljs-keyword">var</span> mouseDownX, mouseDownY <span class="hljs-comment">// &#x56E0;&#x5728;&#x79FB;&#x52A8;&#x4E2D;&#x9700;&#x8BA1;&#x7B97;&#x9F20;&#x6807;&#x7684;&#x504F;&#x79FB;&#x9700;&#x8981;&#x7528;&#x5230;&#x9F20;&#x6807;&#x6309;&#x4E0B;&#x65F6;&#x7684;&#x5750;&#x6807;&#xFF0C;&#x56FA;&#x58F0;&#x660E;&#x79F0;&#x5168;&#x5C40;&#x53D8;&#x91CF;  </span>
    <span class="hljs-comment">//onmousedown&#xFF1A;&#x9F20;&#x6807;&#x6309;&#x4E0B;</span>
    obj.onmousedown = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
    mouseDownX = e.pageX;
    mouseDownY = e.pageY;
}
    <span class="hljs-comment">//onmousemove&#xFF1A;&#x9F20;&#x6807;&#x79BB;&#x5F00;</span>
    obj.onmousemove = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
        <span class="hljs-keyword">var</span> mouseMoveX = e.pageX,
        mouseMoveY = e.pageY;
    }
</code></pre>
<h3 id="&#x62D6;&#x62FD;&#x539F;&#x7406;&#x56FE;">&#x62D6;&#x62FD;&#x539F;&#x7406;&#x56FE;</h3>
<p><img src="../img/js/w.jpg" alt="&#x539F;&#x7406;&#x56FE;"></p>
<pre><code>&#x79FB;&#x52A8;&#x540E;&#x5143;&#x7D20;:
(1)X&#x5750;&#x6807;&#x4E3A; :&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x540E;&#x7684;X&#x5750;&#x6807; - &#x9F20;&#x6807;&#x6309;&#x4E0B;&#x7684;X&#x5750;&#x6807; + &#x5143;&#x7D20;&#x7684;&#x521D;&#x59CB;X&#x5750;&#x6807;
(2)Y&#x5750;&#x6807;&#x4E3A; :&#x9F20;&#x6807;&#x79FB;&#x52A8;&#x540E;&#x7684;Y&#x5750;&#x6807; - &#x9F20;&#x6807;&#x6309;&#x4E0B;&#x7684;Y&#x5750;&#x6807; + &#x5143;&#x7D20;&#x7684;&#x521D;&#x59CB;Y&#x5750;&#x6807;
&#x628A;&#x65B0;&#x7684; X&#xFF0C;Y &#x66FF;&#x6362;&#x5143;&#x7D20;&#x7684; X&#xFF0C;Y &#x5C31;&#x641E;&#x5B9A;&#x4E86;&#x3002;
</code></pre><pre><code>            &lt;script type=&quot;text/javascript&quot;&gt;
                var mouseDownX, mouseDownY, initX, initY, flag = false;
                obj.onmousedown = function(e) {
                    //&#x9F20;&#x6807;&#x6309;&#x4E0B;&#x65F6;&#x7684;&#x9F20;&#x6807;&#x6240;&#x5728;&#x7684;X&#xFF0C;Y&#x5750;&#x6807;  
                    mouseDownX = e.pageX;
                    mouseDownY = e.pageY;

                    //&#x521D;&#x59CB;&#x4F4D;&#x7F6E;&#x7684;X&#xFF0C;Y &#x5750;&#x6807;  
                    initX = obj.offsetLeft;
                    initY = obj.offsetTop;

                    //&#x8868;&#x793A;&#x9F20;&#x6807;&#x5DF2;&#x6309;&#x4E0B;  
                    flag = true;
                }
                obj.onmousemove = function(e) {
                    // &#x786E;&#x4FDD;&#x9F20;&#x6807;&#x5DF2;&#x6309;&#x4E0B;  
                    if(flag) {
                        var mouseMoveX = e.pageX,
                            mouseMoveY = e.pageY;
                        this.style.left = parseInt(mouseMoveX) - parseInt(mouseDownX) + parseInt(initX) + &quot;px&quot;;
                        this.style.top = parseInt(mouseMoveY) - parseInt(mouseDownY) + parseInt(initY) + &quot;px&quot;;
                    }

                }
                obj.onmouseup = function() {
                    //&#x6807;&#x8BC6;&#x5DF2;&#x677E;&#x5F00;&#x9F20;&#x6807;  
                    flag = false;
                }
            &lt;/script&gt;
</code></pre><h3 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h3>
<pre><code>&#x5982;&#x679C;&#x7528;jquery&#x5E93;&#x6765;&#x5199;&#x7684;&#x8BDD;&#x4E09;&#x4E2A;&#x4E8B;&#x4EF6;&#x4E3A;mousedown&#x3001;mousemove&#x3001;mouseup&#xFF0C;&#x540D;&#x79F0;&#x7A0D;&#x5FAE;&#x6709;&#x70B9;&#x5DEE;&#x522B;&#x3002;
&#x8FD8;&#x6709;&#x4E00;&#x70B9;&#xFF0C;&#x88AB;&#x62D6;&#x62FD;&#x7684;&#x5143;&#x7D20;&#x7684;&#x6837;&#x5F0F;&#x8981;&#x8BBE;&#x7F6E;&#x6210;&#x7EDD;&#x5BF9;&#x6216;&#x76F8;&#x5BF9;&#x4F4D;&#x7F6E;&#x624D;&#x6709;&#x6548;&#x679C;&#x3002;
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../MyGitbook_new/js/Dom.html" class="navigation navigation-prev " aria-label="Previous page: Dom入门教程图解"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../MyGitbook_new/js/Braces.html" class="navigation navigation-next " aria-label="Next page: {} 大括号"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
